// miniprogram/pages/fenlei/fenlei.js
function dateFormat(date,formatStriing){
  let year =date.getFullYear()
  formatStriing= formatStriing.replace(/(y+)/i,function($0,$1){
    return year.toString().substr(4-$1.length)})
  let month = date.getMonth()+1
  formatStriing= formatStriing.replace(/(m+)/i,function($0,$1){
    if(month<=9){
    return "0"+month.toString().substr(2-$1.length)}
    else
    return month.toString().substr(2-$1.length)})
  let day = date.getDate()
  formatStriing= formatStriing.replace(/(d+)/i,function($0,$1){
    if(day<=9){return "0"+day.toString().substr(2-$1.length)}else
    return day.toString().substr(2-$1.length)})
  let hour = date.getHours()
  formatStriing= formatStriing.replace(/(h+)/i,function($0,$1){
    return hour.toString().substr(2-$1.length)})
  let minute = date.getMinutes()
  formatStriing= formatStriing.replace(/(t+)/i,function($0,$1){
    return minute.toString().substr(2-$1.length)})
  let second = date.getSeconds()
  formatStriing = formatStriing.replace(/(s+)/i,function($0,$1){
    return second.toString().substr(2-$1.length)})
  return formatStriing
}
let date = new Date()
Date.prototype.format=function(formatStriing){
  return dateFormat(this,formatStriing)
}
//YYYY年MM月DD日hh时tt分ss秒
console.log(dateFormat(date,"YYyy年MM月DD日hh时tt分ss秒"))
console.log(date.format("yyyy年"))
class Car{
  constructor(name,year,color){
  this.name=name
  this.year=year
  this.color=color
  }
}
var qiche = new Car("宝马",19,"black")
console.log(qiche)
///创建随机数组
function buildArray(){
  let array = []
  for(let i=0; i<parseInt(Math.random()*1000);i++) 
  array.push(i)
  return array
}
  ///按分页获取数组中的数据///array 数组
  ///pageNum 开始页从1开始，默认1///pageSize 每页大小，默认10
  ///返回结果示例{total:10,rows:[]} total为数据总数，rows为具体返回的数据
  //[1,2,3,4,5,6,7,8] pageSize=5 pageNum=1
  //{total:8,rows [1,2,3,4,5]}
  function getList(array,pageNum,pageSize){
  let rc={}
  let x=(pageNum-1)*pageSize
  rc.total = array.length
  rc.rows = array.slice(x,x+pageSize)
  return rc
  ///完成数据分页的过程 
}
  let array = buildArray() 
  console.log(array)
  console.log(getList(array,2,5))
//1.生成0- 99的随机数
function random1(){
  let y,x;
  y=100*Math.random();
  x=Math.floor(y);
  return x;
}
console.log(random1())

var students=[
  {
    name:"张三",
    no:"201901"
  },
  {
    name:"李四",
    no:"201902"
  },
  {
    name:"王五",
    no:"201903"
  }
]
function findStudentByNo1(no){
  for(var i=0;i<students.length;i++)
  {
    if (students[i].no==no){
      return students[i].name;
      break;
    }
  }
}
console.log(findStudentByNo1("201901"))
//2

function findStudentByNo2(no){3
  if(!wx.studentObj){
    wx.studentObj={}
    for (let i = 0; i < students.length; i++) {
      let student = students[i]
      wx.studentObj[student.no]=student.name
    }
    console.log(wx.studentObj)
  }
  return wx.studentObj[no]
}
console.log(findStudentByNo2("201901"))
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseURL:wx.baseURL,
    active:0,
    items:[],
    curIndex:0,
    
    
  },
  changcurIndex:function(e){
    var index= parseInt(e.target.dataset.index);
    this.setData({
      curIndex:index
    })
    
  },
  tiaozhuanxiangqing(e){
    console.log(e.currentTarget.dataset.name)
    wx.navigateTo({
      url: '../Product list/Product list?key='+e.currentTarget.dataset.name
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.get('/categorys').then(res=>{
      console.log(res)
      res.sort((a,b)=>{ //将第一级分类放在数组之前
        return a.level - b.level
      })
      let obj={}//缓存 理清数据的关联关系
      for(let i=0;i<res.length;i++){    
        obj[res[i.id]] =res[i]
        let parentId =res[i].parentId,id = res[i].id
        res[i].children = []
        obj[id] =res[i]
        ///通过parentId和id确定关联关系
        if(parentId && obj[parentId]) obj[parentId].children.push(res[i])
      }
      //提取第一级分类
      let items =[]
      for(let key in obj){
        if(obj[key].level ==1) items.push(obj[key])
      }
      console.log(items)
      this.setData({
        items
      })
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})
